package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MenuDao;
import com.itheima.dao.RoleDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = RoleService.class)
@Transactional
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private MenuDao menuDao;
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage, pageSize);
        Page all = roleDao.selectByCondition(queryString);
        List result = all.getResult();
        long total = all.getTotal();
        return new PageResult(total, result);
    }

    @Override
    public void add(Role role, Integer[] permissionIds,Integer[] menuIds) {
        roleDao.add(role);
        Integer roleId = role.getId();
        setRoleAndPermission(roleId, permissionIds);
        setRoleAndMenu(roleId, menuIds);
    }

    public void setRoleAndPermission(Integer roleId, Integer[] permissionIds) {
        if (permissionIds!=null&&permissionIds.length>0) {
            for (Integer permissionId : permissionIds) {
                Map map = new HashMap<>();
                map.put("roleId", roleId);
                map.put("permissionId", permissionId);
                roleDao.setRoleAndPermission(map);
            }
        }
    }
    public void setRoleAndMenu(Integer roleId, Integer[] menuIds) {
        if (menuIds!=null&&menuIds.length>0) {
            for (Integer menuId : menuIds) {
                Map map = new HashMap<>();
                map.put("roleId", roleId);
                map.put("menuId", menuId);
                roleDao.setRoleAndMenu(map);
                //添加父级菜单
                Menu menuById = menuDao.findById(menuId);
                Integer parentMenuId = menuById.getParentMenuId();
//                System.out.println("wwwwwwwwwwwwwwwww" + parentMenuId);
                if (parentMenuId != null && parentMenuId != 0) {
                    Map map1 = new HashMap<>();
                    map1.put("roleId", roleId);
                    map1.put("menuId", parentMenuId);

                    Integer xxx = roleDao.findByMenuIdAndRoleId(map1);
//                    System.out.println("qqqqqqqqqqqqqq"+xxx);
                    if (xxx == null ||xxx==0) {
                        roleDao.setRoleAndMenu(map1);
                    }
                }


            }
        }
    }

    @Override
    public Role findById(Integer roleId) {
        return roleDao.findById(roleId);
    }

    @Override
    public List<Integer> findPermissionIdsByRoleId(Integer roleId) {
        return roleDao.findPermissionIdsByRoleId(roleId);
    }

    @Override
    public List<Integer> findMenuIdsByRoleId(Integer roleId) {
        return roleDao.findMenuIdsByRoleId(roleId);
    }
    

    @Override
    public List<Role> findAll() {
        return roleDao.findAll();
    }


    @Override
    public void edit(Role role, Integer[] perssionIds, Integer[] menuIds) {
        roleDao.edit(role);
        roleDao.deleteRolePermissionAssocication(role.getId());
        roleDao.deleteRoleMenuAssocication(role.getId());
        setRoleAndPermission(role.getId(), perssionIds);
        setRoleAndMenu(role.getId(), menuIds);

    }

    @Override
    public void delete(Integer id) {
        long count = roleDao.findUserCountByRole(id);
        if (count > 0) {
            throw new RuntimeException("当前角色被"+count+"个用户引用,不能删除");
        }
        roleDao.deleteRolePermissionAssocication(id);
        roleDao.deleteRoleMenuAssocication(id);
        roleDao.deleteById(id);
    }
}
